#include "main.h"

void* mob_thread(void *arg)
{
	int newfd = *(int*)arg;
	int ret;
	char buf[size];//接收来自客户端的命令	

	/*线程分离*/
	pthread_detach(pthread_self());

	/*接收命令*/
	bzero(buf, size);
	ret = read(newfd, buf, size);
	if (ret < 0)
		printf("read fail.\n");
	else if (ret == 0)
	{
		printf("Client offline.\n");
		//break;
	}

	//把命令字符串切割成4部分存储
	bzero(mob_str[0], strlen(mob_str[0])); 
	bzero(mob_str[1], strlen(mob_str[1])); 
	bzero(mob_str[2], strlen(mob_str[2])); 
	bzero(mob_str[3], strlen(mob_str[3])); 
	sscanf(buf, "%s%s%s%s", mob_str[0], mob_str[1],mob_str[2], mob_str[3]);

	/*解析命令并调用对应函数*/
	//登录命令
	if (strncmp(mob_str[1], "login", 5)== 0)
		login(newfd,mob_str[2],mob_str[3]);
	//注册命令
	if (strncmp(mob_str[1], "register", 8)== 0)
		regis(newfd,mob_str[2],mob_str[3]);
	//控制设备命令
	else if(strncmp(mob_str[0], "control", 7) == 0)		
		control(newfd,mob_str[2], mob_str[3]);
	//获取参数请求
	else if (strncmp(mob_str[1], "monitor", 12) == 0)
		req_paras(newfd,mob_str[2], mob_str[3]);//服务器判断客户端是否有权限获取参数
	//获取参数正文
	else if(strncmp(mob_str[1], "monitor_info", 12) == 0)
		get_paras(newfd);
	else if(strncmp(mob_str[1], "stop_moninfo_send", 17) == 0)
		printf("收到客户端获取参数结束请求\n");
	else if(strncmp(mob_str[1], "quit", 4) == 0)
		printf("Client offline.\n");

	/*挂断*/
	close(newfd);

	return NULL;
}

